//
//  MyGroupListViewController.m
//  Yiqidianyin
//
//  Created by apple on 15/8/27.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MyGroupListViewController.h"
#import "EaseMob.h"
#import "MBProgressHUD.h"
#import "UIViewController+HUD.h"

@interface MyGroupListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *firend_arr;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MyGroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.tableFooterView = [[UIView alloc]init];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self getFirendList];
    
}


-(void)getFirendList{
    [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsListWithCompletion:^(NSArray *groups, EMError *error) {
        if (!error) {
           
            self.firend_arr = [NSMutableArray array];
            
            [self.firend_arr addObjectsFromArray:groups];
            
            [self.tableView reloadData];
        }
    } onQueue:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.firend_arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyGroupListCell" forIndexPath:indexPath];
    
    EMBuddy * buddy = self.firend_arr[indexPath.row];
//    NSLog(@"%@",buddy.username);

    NSString *str = buddy.username;
    NSLog(@"%@",str);
    
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"删除";
    
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    EMGroup *group = self.firend_arr[indexPath.row];
    EMError * error;
    BOOL delete = [[EaseMob sharedInstance].chatManager removeBuddy:group.groupId removeFromRemote:YES error:&error];
    if (delete) {
        
        
        [[EaseMob sharedInstance].chatManager removeConversationByChatter:group.groupId deleteMessages:YES append2Chat:NO];
        
        [self.firend_arr removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];

        [self getFirendList];
        
        
    }else{
        
        [self showHint:@"删除失败"];
        
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EMGroup *group = self.firend_arr[indexPath.row];
    
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController * vc = [story instantiateViewControllerWithIdentifier:@"groutChat"];
    
    EMConversation * conversantion =
    [[EaseMob sharedInstance].chatManager conversationForChatter:group.groupId conversationType:eConversationTypeChat];
    
    [vc setValue:conversantion forKey:@"conversation"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
